from config.db import DB_CONFIG
from py3db import mysql


class AppData:
    def __init__(self):
        pass

    @classmethod
    def create_connect(cls):
        return mysql.MySql(DB_CONFIG['host'], DB_CONFIG['user'], DB_CONFIG['password'], "crawler")

    def get_one_info(self, public_code):
        db = self.create_connect()
        app_result = db.select('crawler_app', condition_dict={'public_code': public_code})
        app_id = app_result[0][0] if len(app_result) else None
        layout_result = db.select('crawler_layout', condition_dict={'app_id': app_id})
        field_list = []
        for layout in layout_result:
            layout_id = layout[0]
            field_result = db.select("crawler_field", condition_dict={'layout_id': layout_id})
            field_list.extend(list(field_result))
        app_msg = app_result[0] if len(app_result) else None
        db.close_connect()
        return app_msg, layout_result, field_list

    def get_layout_info_by_id(self, layout_id):
        db = self.create_connect()
        layout_list = db.select('crawler_layout', condition_dict={'id': layout_id})
        db.close_connect()
        return layout_list[0] if len(layout_list) else None


if __name__ == '__main__':
    data = AppData()
    print(data.get_one_info('笔趣阁'))
